<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/3/13
 * Time: 12:15
 */
class WjbSeoAction extends UserAction{

    private $data = array();

    public function __construct(){
        parent::_initialize();
        $this->canUseFunction('wjbseo');
    }

    public function index(){
        if( IS_POST ) {
            $param = $this->createParam();

            $info = $this->curlPost('http://wxapi.711688.com.cn/wx/product/?action=syn', $param);

            $this->processInfo($info);

            $r = $this->processData();

            if (!$r) {
                $this->error("数据保存失败");
            }
        }else {

            $this->display();
        }
    }


    //接口相关参数
    public function createParam(){
        $data = array();
        $data['pkid'] = $this->createpkid();

        $res = M('seo_user')->where(array('token'=>$this->token))->find();
        if(!$res){
            $this->error("你还没有进行推广绑定......");
            exit;
        }
        $data['name'] = $res['name'];
        $data['pass'] = $res['pass'];

        $data['title'] = $_POST['title'];


        $keywordsArr = array();
        for( $i = 0; $i < 10; $i++ ){
            if( $_POST['keywords'.$i] !== '' ) {
                array_push($keywordsArr,$_POST['keywords'.$i]);
            }
        }
        $keywords = '';
        foreach($keywordsArr as $key) {
            $keywords .= $key . ',';
        }
        $data['keywords'] = trim( $keywords, ',' );
        if($data['keywords'] == ''){
            $this->error("至少一个关键词  建议10个");
            exit;
        }

        $data['imgurl'] = $_POST['imgurl'];
        $data['introl'] = $_POST['introl'];
        $data['content'] = $_POST['content'];

        if(  $data['title'] == '' ||  $data['content'] == '') {
            $this->error("标题,内容不能为空..");
            exit;
        }

        $param = 'Pk_id='.$data['pkid'];
        $param .= '&name='.$data['name'];
        $param .= '&pass='.$data['pass'];
        $param .= '&title='.$data['title'];
        $param .= '&keywords='.$data['keywords'];

        if( $data['imgurl'] !== '' ) {
            $param .= '&imgurl=' . $data['imgurl'];
        }
        if( $data['introl'] !== '' ) {
            $param .= '&introl=' . $data['introl'];
        }

        $param .= '&content='.$data['content'];

        $this->data = $data;

        return $param;
    }


    public function createpkid(){
        $pkid  = time();
        $str = '0123456789';
        for($i = 0; $i < 10; $i++ ) {
            $pkid .= substr($str,rand(0,9),1);
        }

        return $pkid;
    }


    /**
     *
     * @param unknown $url
     * @param string $data
     * @return mixed
     */
    public function curlPost($url,$data=null){
        $curl = curl_init(); 
        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
        if($data != null){
            curl_setopt($curl, CURLOPT_POST, 1); 
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 300); 
        curl_setopt($curl, CURLOPT_HEADER, 0); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
        $info = curl_exec($curl); 
        return $info;
    }

    /**
     * 绑定用户名
     */
    public function  binduser(){

        if( IS_POST ){
            $data['name'] = $_POST['name'];
            $data['pass'] = $_POST['pass'];
            $data['token'] = $this->token;
            if($data['name'] == '' || $data['pass'] == '' ){
                $this->error("key1 key2 均不能为空");
            }
            $r = M('seo_user')->where(array('token'=>$this->token))->find();

            if( !$r ){
                M('seo_user')->add($data);
                $this->success("绑定成功");
            }else{
                M('seo_user')->where(array('token'=>$this->token))->save($data);
                $this->success("更新成功");
            }
            exit;
        }
        $res = M('seo_user')->where(array('token'=>$this->token))->find();
        $this->assign('binduser',$res);
        $this->display();
    }


    /**
     * @param $info
     * 处理返回值
     */
    public function processInfo( $info ){

        $json = json_decode( $info,true );

        if( !$json ){
            $this->error( "发送失败 请联系管理员" );
            exit;
        }

        $code = $json['code'];
        $msg = $json['msg'];
        $status = $json['status'];
        if( $status == 'success' || $code == 0){
            $this->success("产品添加成功");
        }else {
            $arr = array(11, 12, 13, 15, 16, 500);
            if (in_array($code, $arr)) {
               $this->error($msg);
            }
        }
    }

    /**
     * 处理数据  保存
     * @return bool
     */
    public function processData(){

        $this->data['token'] = $this->token;

        $res = M('seo_content')->add($this->data);

        if( $res ){
            return true;
        }
        return false;
    }

    public function msglist(){

        import('ORG.Util.Page');

        $count = M('seo_content')->count();

        $page =  new Page($count);

        $show  = $page->show();// 分页显示输出

        $msglist = M('seo_content')->where(array('token'=>$this->token))->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign( 'msglist', $msglist );

        $this->assign('page',$page);

        $this->display();
    }

    public function msgcontent(){

        $id = $_GET['id'];

        $msg = M('seo_content')->where(array('token'=>$this->token))->find();

        $this-> assign( 'msg', $msg );

        $this->display();
    }

}